var app = new Vue({
	el: '#app',
	data: {
		newsInfo:{},	//新闻信息
		newsReplys:[],	//新闻回复列表
		newsReplyss:[],	//回复的回复列表
		replyData:{content:'',content1:''},	//回复内容
		replyCount : 0,
		infos:{
			user:globalData.userInfo
		},//用户的所有信息
		url:{
			getNewInfo:'/api/News/GetNews',
			getPageNewsCommentForApp:'/api/Forum/GetPageNewsCommentForApp',
			like:'/api/News/Like', //赞和采
			addForumFavorites: '/api/MyFavorites/AddNewsFavorites', //收藏帖子
			reply:'/api/Forum/AddForum',
			getPageUserCommentForApp:'/api/Forum/GetPageUserCommentForApp',//回复的回复
		},
		page: {
				pageIndex: 1,
				pageSize: 10,
				pageTotal: 0
		},
		replyId:"",
		children_data:{
			replyId:'',
			isShow:false
		}
	},
	mounted: function() {
		var that = this 
		globalData.getTempToken(function(){
			
			$(document).on('click',function(){
				$('.back_other').slideUp()
			})
			var req = globalData.utils.getUrl();
			var id = req.id;
			that.getNewInfo(id);
			that.getReplys(id);
		})
		
	},
	methods: {
		//对回复的回复
		replyReplyShow:function(ev){
			var dom = $(ev.target);
			console.log(dom)
			while(!dom.hasClass('one_content')){
				dom = dom.parent();
			}
			var sibling = dom.next('.back_other')
			if(sibling.css('display') == 'block'){
				sibling.slideUp();
			}else{
				sibling.slideDown();
			}
		},
		//回复
		reply:function(id,num){
			var b = globalData.userIsLogin();
			var that = this
			if(!b)
				return;
			var content = num==0?this.replyData.content:this.replyData.content1;
			if(!content){
				$("#alert").modal()
				$('#alert p').text('请输入评论内容')
				return;
			}
			globalData.ajaxPost(this.url.reply,{ReplyId:id,Title:this.newsInfo.Title,Content:content},function(res){
				if(!res.suc){
					globalData.utils.alert(res.msg);
					return;
				}
				$(document).click();
				if(num==0)
					that.replyData.content="";
				else if(num==1)
					that.replyData.content1="";
				that.getReplys(that.newsInfo.ID);
			});
		},
		//得到新闻信息
		getNewInfo:function(id){
			var that = this;
			globalData.ajaxPost(this.url.getNewInfo,{"ID":id},function(res){
				//隐藏loading
				$('.loading').hide()
				var d = res.data;
				d = globalData.utils.notNullDefaulst(d,{});
				d.Image=globalData.utils.imagePrefix(d.Image);
				d.Link = globalData.utils.notNullDefaulst(d.Link,0);
				d.Unlike = globalData.utils.notNullDefaulst(d.Unlike,0);
				that.newsInfo=d;
				console.log(d)
			});
		},
		//得到新闻回复
		getReplys:function(id){
			this.getPageReplys(this.page.pageIndex,this.page.pageSize,id);
//			var that = this;
//			globalData.ajaxPost(this.url.getPageNewsCommentForApp,{PageIndex:1,PageSize:100,"ID":id},function(res){
//				var d = res.data;
//				d = globalData.utils.notNullDefaulst(d,[]);
//				that.newsReplys=d;
//				that.replyCount=res.count;
//			});
		},
		getPageReplys:function(curPage, pageSize,id){
			var that = this;
			id = id==null?this.newsInfo.ID:id;
			var data = {
				PageIndex: curPage,
				PageSize: pageSize,
				ID:id
			};
			globalData.ajaxPost(this.url.getPageNewsCommentForApp,data,function(res){
				var d = res.data;
				d = globalData.utils.notNullDefaulst(d,[]);
				globalData.utils.page(that, curPage, res.count);
				for(var i=0;i<d.length;i++){
					d[i].HeadImage=globalData.utils.imagePrefix(d[i].HeadImage);
				}
				that.newsReplys=d;
				that.replyCount=res.count;
			});
		},
		//收藏帖子
		addForumFavorites: function() {
			var d = this.newsInfo;
			if(d.isFavorite)
				return;
			var that = this;
			var b = globalData.userIsLogin(this);
			if(!b)
				return;
			var user = globalData.getUserInfo();
			var data = {
				'UserID': user.ID,
				'ForumID': d.ID
			};
			globalData.ajaxPost(this.url.addForumFavorites, data, function(res) {
				if(res.suc) {
					d.isFavorite = true;
					d.FavoriteCount++
//					globalData.utils.alert("收藏成功");
				}
			});
		},
		//赞和踩
		like: function(num) {
			var d = this.newsInfo;
			var that = this;
			var b = globalData.userIsLogin(this);
			if(!b)
				return;
			if(d.isLike!=null)
				return;
			var data = {
				ID: d.ID,
				IsLike: num
			};
			globalData.ajaxPost(this.url.like, data, function(res) {
				if(!res.suc)
					return;
				that.getNewInfo(that.newsInfo.ID);
			});
		},
		//回复详情列表
		moreReply:function(id){
			var that = this
			that.children_data.replyId = id
			that.children_data.isShow = true
		},
	}
})